package com.liveinstars.im.domain.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liveinstars.im.domain.dao.mapper.CandyKidsRecordMapper;
import com.liveinstars.im.domain.dao.model.CandyKidsRecord;
import com.liveinstars.im.domain.manager.CandyKidsRecordManager;
import com.onepiece.shipelves.common.constant.Constant;
import com.onepiece.shipelves.common.enums.SystemExceptionEnum;
import com.onepiece.shipelves.common.exception.DatabaseException;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 糖果分配表 服务实现类
 * </p>
 *
 * @author 8btc
 * @since 2020-03-24
 */
@Service
public class CandyKidsRecordManagerImpl extends ServiceImpl<CandyKidsRecordMapper, CandyKidsRecord> implements CandyKidsRecordManager {

    private final static Logger logger = LoggerFactory.getLogger(CandyKidsRecordManagerImpl.class);

    @Override
    public List<CandyKidsRecord> listByBoxIdAndStatus(Long candyBoxId, Integer status) {
        QueryWrapper<CandyKidsRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("box_id", candyBoxId);
        queryWrapper.eq("status", status);
        List<CandyKidsRecord> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public Boolean updateUserIdByBoxIdAndNo(Long userId, Long boxId, Integer no) {
        CandyKidsRecord candyKidsRecord = new CandyKidsRecord();
        candyKidsRecord.setStatus(Constant.YES);
        candyKidsRecord.setUserId(userId);
        QueryWrapper<CandyKidsRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("box_id", boxId);
        queryWrapper.eq("no", no);
        queryWrapper.eq("status", Constant.NO);
        return this.update(candyKidsRecord, queryWrapper);
    }

    @Override
    public CandyKidsRecord getOneByBoxIdAndNo(Long boxId, Integer no) {
        QueryWrapper<CandyKidsRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("box_id", boxId);
        queryWrapper.eq("no", no);
        List<CandyKidsRecord> list = this.list(queryWrapper);
        if(CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public void batchInsert(List<CandyKidsRecord> list) {
        try {
            this.saveBatch(list);
        } catch (Exception e) {
            logger.error("获取举报记录分页列表异常：{}", e.getMessage() , e);
            throw new DatabaseException(SystemExceptionEnum.MYSQL_OPERATING_ERROR);
        }
    }
}
